<?php


class Manage extends AdminBase {

	//管理员列表
	public function index(){
		$data['list']=$this->db->get('admin')->result_array();
		$this->load->view('manage/index',$data);

	}

	//添加管理员
	public function add(){
		if ($this->post()){
			$post=$this->input->post();
			$post['password']=md5($post['password'].'yw');
			$post['create_time']=time();
			$post['update_time']=time();
			if($this->check($post['account'])){
				$this->show(['code'=>0,'msg'=>'管理员已存在']);
			}
			$res=$this->db->insert('admin',$post);
			if ($res){
				$this->show(['code'=>1,'msg'=>'添加成功']);
			}else{
				$this->show(['code'=>0,'msg'=>'添加失败']);
			}
		}
		$data['menu']=$this->GetRole();
		$this->load->view('manage/add',$data);
	}

	//编辑管理员
	public function edit(){
		if ($this->post()){
			$post=$this->input->post();
			$post['update_time']=time();
			$res=$this->db->update('admin', $post,array('id'=>$post['id']));
			if ($res){
				$this->show(['code'=>1,'msg'=>'更新成功']);
			}else{
				$this->show(['code'=>0,'msg'=>'更新失败']);
			}
		}
		$id=$this->GetId();
		$data['list']=$this->db->where('id',$id)->get('admin')->row_array();
		$data['menu']=$this->GetRole();
		$this->load->view('manage/edit',$data);
	}

	//删除管理员
	public function del(){
		$id=$this->PostId();
		$res=$this->db->delete('admin', array('id'=>$id));
		if ($res){
			$this->show(['code'=>1,'msg'=>'删除成功']);
		}else{
			$this->show(['code'=>0,'msg'=>'删除失败']);
		}
	}


	//检查管理员是否存在
	public function check($account){
		$check=$this->db->where('account',$account)->get('admin')->row_array();
		if ($check){
			return  true;
		}else{
			return false;
		}
	}





}
